<?php
require_once dirname(__FILE__) . '/libraries/ServiceBase.php';
/**
 * Protect Service （防沉迷）
 *
 */
class SDOServiceProtect extends ServiceBase
{
    # 正式环境添加防沉迷
    const PROTECT_OP_URL = 'PROTECT_OP_URL';
    # 测试环境添加防沉迷
    const PRE_PROTECT_OP_URL = 'PRE_PROTECT_OP_URL';
    # 正式环境添加防沉迷
    const SELF_PROTECT_OP_URL = 'SELF_PROTECT_OP_URL';
    # 测试环境添加防沉迷
    const PRE_SELF_PROTECT_OP_URL = 'SELF_PROTECT_OP_URL';
    /**
     * 开通SDO防沉迷
     *
     * @param array|string $message // array(code,msg)
     * @param int $appId
     * @param string $env
     * @return boolean
     */
    public static function addProtect (&$message, $appId, $env = 'pre')
    {
        $op = 'add';
        $url = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_PROTECT_OP_URL) : sdk_fetch_config(
        SDK_ENV . self::PROTECT_OP_URL);
        $url .= $op;
        $param['appId'] = $appId;
        //print_r($param);
        $content = self::getRestfulContent($url . '&' . http_build_query($param), $message);
        if ($content === FALSE) {
            /*$message = array(
                '-2', 
                $message
            );*/
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\texception: ".$message, __CLASS__);
            return FALSE;
        }
        $result = explode('^$^', $content);
        if ($result[0] != - 610901) {
        	$message = count($result) == 2 ? $result[1] : '接口错误';
        	self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".$content, __CLASS__);
        	return false;
        }
        self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".$content, __CLASS__, true);
        return true;
    }    
    /**
     * 开通查询用户身份证信息接口
     * @param string $message 错误信息 
     * @param int $appId 应用ID
     * @param string $env 环境
     */
    public static function addSelfProtect(&$message, $appId, $env = 'pre')
    {
        $op = 'add';
        $url = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_SELF_PROTECT_OP_URL) : sdk_fetch_config(
        SDK_ENV . self::SELF_PROTECT_OP_URL);
        $url .= $op;
        $param['appId'] = $appId;
        //print_r($param);
        $content = self::getRestfulContent($url . '&' . http_build_query($param), $message);
        if ($content === FALSE) {
            /*$message = array(
                '-2', 
                $message
            );*/
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\texception: ".$message, __CLASS__);
            return FALSE;
        }
        $result = explode('^$^', $content);
        if ($result[0] != -612001) {
        	$message = count($result) == 2 ? $result[1] : '接口错误';
        	self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".$content, __CLASS__);
        	return false;
        }
        self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".$content, __CLASS__, true);
        return true;
    }
    /**
     * 查询SDO防沉迷
     *
     * @param array|string $message // array(code,msg)
     * @param int $appId
     * @param string $env
     * @return array|boolean
     */
    public static function searchProtect (&$message, $appId, $env = 'pre')
    {
        $op = 'search';
        $url = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_PROTECT_OP_URL) : sdk_fetch_config(
        SDK_ENV . self::PROTECT_OP_URL);
        $url .= $op;
        $param['appId'] = $appId;
        $content = self::getRestfulContent($url . '&' . http_build_query($param), $message);
        if ($content === FALSE) {
            return FALSE;
        }
        $result = explode('^$^', $content);
        if ($result[0] != - 611001) {
        	$message = in_array(count($result), array(2, 4)) ? $result[1] : '接口错误';
        	self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".$content, __CLASS__);
        	return false;
        }
        
        return true;
    }
	/**
     * 自检函数
     */
    public static function selfTest() 
    {
    	$arr = array(
        	sdk_fetch_config(SDK_ENV . self::PROTECT_OP_URL),
        	sdk_fetch_config(SDK_ENV . self::PRE_PROTECT_OP_URL)
    	);
    	return self::curlTest($arr);
    }
}
